﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Point = Microsoft.Xna.Framework.Point;

using Microsoft.Xna.Framework.Input;
using SplashOn_PhoneLib;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Navigation;
using Microsoft.Xna.Framework.Input.Touch;

namespace SplashOn_Phone
{
    public partial class ObjectPicker : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        UIElementRenderer elementRenderer;
        SpriteBatch spriteBatch;

        SplashOn_ObjectManipulation splashOn_Object;

        bool mouse = true;

        float rot1 = 0;
        float rot2 = 0;
        float rot3 = 0;
        int direct = 0;

        public ObjectPicker()
        {
            InitializeComponent();
            this.DataContext = this;

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;

            splashOn_Object = new SplashOn_ObjectManipulation();

            LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);

        }

        void GamePage_LayoutUpdated(object sender, EventArgs e)
        {
            // Create the UIElementRenderer to draw the XAML page to a texture.

            // Check for 0 because when we navigate away the LayoutUpdate event
            // is raised but ActualWidth and ActualHeight will be 0 in that case.
            if (ActualWidth > 0 && ActualHeight > 0 && elementRenderer == null)
            {
                elementRenderer = new UIElementRenderer(this, (int)640, (int)480);
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            TouchPanel.EnabledGestures = GestureType.Flick | GestureType.Tap | GestureType.FreeDrag | GestureType.Pinch | GestureType.DoubleTap;
            splashOn_Object.Initialize(SharedGraphicsDeviceManager.Current, contentManager);
            timer.Start();  // Start the timer
            base.OnNavigatedTo(e);
            try
            {
                NavigationService.RemoveBackEntry();
            }
            catch (InvalidOperationException exe)
            {
                DebugHelper.w(exe.Message);
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            timer.Stop();
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            splashOn_Object.Dispose();
            base.OnNavigatedFrom(e);
        }

        public void MouseClickHandler()
        {
            MouseState ms = Mouse.GetState();
            Point MouseLocation = new Point();
            MouseLocation.X = ms.X;
            MouseLocation.Y = ms.Y;

            while (true)
            {
                //splashOn_Object.MouseInputInit();
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    System.Diagnostics.Debug.WriteLine("Pressed");
                    splashOn_Object.MouseClickHandler(1, MouseLocation);
                }
            }

        }
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            HandleTouchInput();
            splashOn_Object.Update(e.ElapsedTime, this.IsEnabled);
        }

        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            //SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            //.Render();

            if (splashOn_Object.VideoBackground == null)
                splashOn_Object.VideoBackground = elementRenderer.Texture;

            splashOn_Object.Draw(e.ElapsedTime);
        }

        private void HandleTouchInput()
        {
            TouchCollection touches = TouchPanel.GetState();

            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();

                switch (gesture.GestureType)
                {
                    case GestureType.Flick:
                        //System.Diagnostics.Debug.WriteLine(":: gesture.Delta.X=" + gesture.Delta.X);
                        //System.Diagnostics.Debug.WriteLine(":: gesture.Delta.Y=" + gesture.Delta.Y);
                        if (gesture.Delta.X > 500)     // to go LEFT
                        {
                            //NavigationService.Navigate(new Uri(string.Format("/GamePage.xaml"), UriKind.Relative));
                            //NavigationService.RemoveBackEntry();

                        }
                        else if (gesture.Delta.X < -1000)   // to go RIGHT
                        {

                        }
                        break;

                    case GestureType.Tap:
                        //System.Diagnostics.Debug.WriteLine(":: tap gesture.Delta=" + gesture.Delta.ToString());
                        //System.Diagnostics.Debug.WriteLine(":: tap gesture.Position=" + gesture.Position.ToString());

                        //System.Diagnostics.Debug.WriteLine(":: tap gesture.X=" + gesture.Position.X.ToString());
                        //System.Diagnostics.Debug.WriteLine(":: tap gesture.Y=" + gesture.Position.Y.ToString());



                        if (gesture.Position.X < 85 && gesture.Position.X > 5 && gesture.Position.Y < 480 && gesture.Position.Y > 415)
                        {
                            splashOn_Object.GetGeo();
                            splashOn_Object.GetTrans();
                            //NavigationService.Navigate(new Uri(string.Format("/ColorPicker.xaml"), UriKind.Relative));
                            NavigationService.Navigate(new Uri(string.Format("/GamePage.xaml"), UriKind.Relative));
                        }
                        if (gesture.Position.X < 800 && gesture.Position.X > 600 && gesture.Position.Y < 480 && gesture.Position.Y > 400)
                        {

                            direct = direct + 1;
                            if (direct == 4)
                            {
                                direct = 0;
                            }
                            System.Diagnostics.Debug.WriteLine(direct);
                            splashOn_Object.GetDelta(rot1, rot2, rot3, direct);
                        }
                        else
                        {
                            if (mouse && splashOn_Object.getObjectSelected() < 0)
                            {
                                Point MouseLocation = new Point();
                                MouseLocation.X = (int)gesture.Position.X;
                                MouseLocation.Y = (int)gesture.Position.Y;
                                //System.Diagnostics.Debug.WriteLine("Pressed");
                                splashOn_Object.MouseClickHandler(1, MouseLocation);
                            }
                        }
                        break;
                    case GestureType.FreeDrag:
                        System.Diagnostics.Debug.WriteLine("FreeDrag");
                        if (direct == 0)
                        {
                            Vector2 vec1 = new Vector2();
                            vec1.X = gesture.Delta.X;
                            vec1.Y = gesture.Delta.Y;
                            System.Diagnostics.Debug.WriteLine("Vec" + vec1.ToString());
                            splashOn_Object.GetDelta(vec1);
                        }

                        break;

                    case GestureType.Pinch:
                        if (direct == 0)
                        {
                            Vector2 pos1 = new Vector2();
                            pos1 = gesture.Position;
                            Vector2 pos2 = new Vector2();
                            pos2 = gesture.Position2;

                            Vector2 oldpos1 = new Vector2();
                            Vector2 oldpos2 = new Vector2();
                            oldpos1 = pos1 - gesture.Delta;
                            oldpos2 = pos2 - gesture.Delta2;
                            float dis = Vector2.Distance(pos1, pos2);
                            float olddis = Vector2.Distance(oldpos1, oldpos2);
                            float fac = dis - olddis;
                            //System.Diagnostics.Debug.WriteLine("pos1" + pos1.ToString());
                            //System.Diagnostics.Debug.WriteLine("pos2" + pos2.ToString());
                            //System.Diagnostics.Debug.WriteLine("oldpos1" + oldpos1.ToString());
                            //System.Diagnostics.Debug.WriteLine("oldpos2" + oldpos2.ToString());
                            //System.Diagnostics.Debug.WriteLine("fac:" + fac.ToString());

                            splashOn_Object.GetDelta(fac);
                        }
                        else
                        {
                            if (direct == 1)
                            {
                                rot1 = gesture.Position.X - gesture.Position2.X;
                                System.Diagnostics.Debug.WriteLine("facX:" + rot1.ToString());
                                System.Diagnostics.Debug.WriteLine("facX:" + rot2.ToString());
                                splashOn_Object.GetDelta(rot1, rot2, rot3, direct);
                            }
                            if (direct == 2)
                            {
                                rot2 = gesture.Position.X - gesture.Position2.X;
                                System.Diagnostics.Debug.WriteLine("facX:" + rot1.ToString());
                                System.Diagnostics.Debug.WriteLine("facX:" + rot2.ToString());
                                splashOn_Object.GetDelta(rot1, rot2, rot3, direct);
                            }
                            if (direct == 3)
                            {
                                rot3 = gesture.Position.X - gesture.Position2.X;
                                // System.Diagnostics.Debug.WriteLine("facX:" + rot1.ToString());
                                //System.Diagnostics.Debug.WriteLine("facX:" + rot2.ToString());
                                splashOn_Object.GetDelta(rot1, rot2, rot3, direct);
                            }

                        }
                        break;


                }
            }
        }
    }
}